//
//  ReplicatorView.swift
//  CAReplicatorLayer
//
//  Created by YYKJ on 2021/4/9.
//

import Foundation
import UIKit

let timePadding = 0.2
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height

class ReplicatorView: UIView {
    
    let replicatorLayerX: CAReplicatorLayer = {
        let layer = CAReplicatorLayer()
        layer.frame = CGRect(x: 0, y: 0, width:screenWidth-100 , height: 24)
        layer.instanceCount = 10
        layer.instanceColor = UIColor.lightGray.cgColor
        layer.instanceDelay = timePadding
        var transform = CATransform3DIdentity
        transform = CATransform3DTranslate(transform, 24, 0, 0)
        layer.instanceTransform = transform
        return layer
    }()
    
    let replicatorLayerY: CAReplicatorLayer = {
        let layer = CAReplicatorLayer()
        layer.instanceCount = 6
        layer.instanceColor = UIColor.lightGray.cgColor
        layer.instanceDelay = 10 * timePadding
        var transform = CATransform3DIdentity
        transform = CATransform3DTranslate(transform, 0, 24, 0)
        layer.instanceTransform = transform
        return layer
    }()
    
    let circleLayer: CAShapeLayer = {
        let layer = CAShapeLayer()
        layer.frame = CGRect(x: 4, y: 0, width: 16, height: 16)
        layer.backgroundColor = UIColor.lightGray.cgColor
        layer.cornerRadius = 8.0
        return layer
    }()
    
    let basicAnimation: CABasicAnimation = {
        let animation = CABasicAnimation()
        animation.keyPath = "backgroundColor"
        animation.duration = 0
        animation.fromValue = UIColor.black.cgColor
        animation.toValue = UIColor.yellow.cgColor
        animation.repeatCount = 0
        animation.autoreverses = false
        animation.isAdditive = true
        animation.isRemovedOnCompletion = false
        animation.fillMode = .forwards
        return animation
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    func setUpView() {
        replicatorLayerX.addSublayer(circleLayer)
        replicatorLayerY.addSublayer(replicatorLayerX)
        self.layer.addSublayer(replicatorLayerY)
        
        circleLayer.add(basicAnimation, forKey: "basicAnimation")
        
        let startButton = UIButton(frame: CGRect(x: 0, y: 300-40, width: 240, height: 40))
        startButton.setTitle("start", for: .normal)
        startButton.setTitleColor(.white, for: .normal)
        startButton.addTarget(self, action: #selector(buttonClick), for:.touchUpInside)
        self.addSubview(startButton)
    }
    
    override func layoutSubviews() {
        self.frame = CGRect(x: (screenWidth - 240) / 2, y:(screenHeight - 300) / 2, width:240, height:300)
        replicatorLayerY.frame = self.bounds
    }
    
    @objc func buttonClick() {
        circleLayer.removeAllAnimations()
        circleLayer.add(basicAnimation, forKey: "basicAnimation")
    }
}
